package com.imu.purse.modules.base.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.imu.constant.Constant;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.base.dao.DictMapper;
import com.imu.purse.modules.base.entity.Dict;
import com.imu.purse.modules.base.service.DictService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 字典分组
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2017-11-03 20:34:20
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class DictServiceImpl extends BaseService<Dict> implements DictService
{
	@Override
    @Transactional
    public int save(final Dict sysDict)
	{
		sysDict.setCs(System.currentTimeMillis());
    	sysDict.setMs(System.currentTimeMillis());
    	return super.insert(sysDict);
	}
	
	@Override
	@Transactional
	/*@Caching(
		evict = {
			@CacheEvict(value = CACHE, key = CACHE_KEY),
		    @CacheEvict(value = CACHE, key = "#sysDict.getId()+" + CACHE_INFO_KEY)
	    }
	)*/
	@CacheEvict(value = CACHE, key = CACHE_INFO_KEY + "+#sysDict.getId()")
    public int update(final Dict sysDict)
    {
		//删除缓存信息
		redisTemplate.delete(Constant.SYS_DICT);
		//更新数据
    	sysDict.setMs(System.currentTimeMillis());
    	return mapper.updateByPrimaryKeySelective(sysDict);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final Dict sysDict)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(sysDict)))
    	{
        	len = this.insert(sysDict);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
 	@CacheEvict(value = CACHE, allEntries = true)
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}

		//删除缓存信息
		redisTemplate.delete(Constant.SYS_DICT);
    	return true;
    }
    
    @Override
    @Cacheable(value = CACHE, key = CACHE_INFO_KEY + "+#id")
    public Dict findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<Dict> queryList(final com.imu.purse.modules.base.entity.Dict sysDict)
    {
    	if (sysDict.getPage() != null && sysDict.getLimit() != null)
    	{
            PageHelper.startPage(sysDict.getPage(), sysDict.getLimit());
        }
    	
    	return mapper.findByPage(sysDict);
    }
    
	@Autowired
	private DictMapper mapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

	/**
     * 缓存的key
     */
    public static final String CACHE = "dictCache";
	
    /**
     * 缓存的key
     */
    public static final String CACHE_INFO_KEY = "\"DICT\"";
}
